package com.zwz.crawler.mangeInfo;

import com.zwz.constant.Constant;
import com.zwz.parse.JsonParserUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.IOException;

import static com.zwz.constant.Constant.ENTERPRISE_ID;

@Slf4j
public class TungeeInvoiceBankQuery {
    public static void startTungeeInvoiceBankQuery( ) {
        // 构建带参数的URL
//        String enterpriseId = "577de19eadf21dc4";
        String url = "https://sales.tungee.com/api/enterprise/detail/invoice-bank-page"
                + "?enterprise_id=" + ENTERPRISE_ID;

        try {
            Connection.Response response = Jsoup.connect(url)
                    .method(Connection.Method.GET)
                    // 设置完整请求头（严格匹配浏览器格式）
                    .header("authority", "sales.tungee.com")
                    .header("accept", "*/*")
                    .header("accept-encoding", "gzip, deflate, br, zstd")
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("cache-control", "no-cache")
                    .header("pragma", "no-cache")
                    .header("priority", "u=1, i")
                    .header("referer", "https://sales.tungee.com/enterprise-details/577de19eadf21dc4/enterprise-information/bussinesss-information")
                    .header("sec-ch-ua", "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("sec-fetch-dest", "empty")
                    .header("sec-fetch-mode", "cors")
                    .header("sec-fetch-site", "same-origin")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36")
                    .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
                    // 设置完整Cookie（需替换为浏览器最新值）
//                    .header("cookie", "_tx_pid=67864ea75f69e05d3b544ca7; __wpkreporterwid_=cc4a07fc-7bb9-40a4-96bc-2cbac34b658f; __last_enter_version=sales; _co_i=6805a5576eceb9637696f09d; _tx_uid=680701dddca941522b77e864; _tx_cid=6805a5576eceb9637696f09d; DISTINCT_ID=b84fe276-099f-4932-92b3-d54b92ae384b; _tx_sid=MTc0NTMwMTE0MzYyMjo4MmQ2OTQ5MTY0NjU2ODNmYjQ5MjlmMjc3NDg5ZDFlZg==; Hm_lvt_f2ee75449fc055cc4dbceb4fe403bea3=1745301094,1745302709; HMACCOUNT=B4E402DF057C895E; acw_tc=0a47308217453109090526200e00732035bfa2f54e47a00fa681298170c3dd; SecurityCenterDuId=ImFBSG15RzdPdVdPdCtsWnNGMFVuSXhVPSI.GujnjA.YBo4Zvc8vs4jgEmtioacknOQ9pE; tg_referrer_source=https%3A%2F%2Fuser.tungee.com%2F; Hm_lpvt_f2ee75449fc055cc4dbceb4fe403bea3=1745311256; accountCenterSessionId=.eJwlzsFKxDAQgOF3yXkPM5lpkt2b2sWL215WhF7KJJlQS6nQdlUU392i95-P_9v0ZdF1MKdtuenBrK_ZnExbD0PzlbCrn7e27obuAaC9NuPTy_mzebxszXhXXa7d1NRn6sb7wRzMrJr7Vd613976HM2pyLTuYP8HklAoATGCUnaUC5bgUDFr1Fg8eiUJycYMLKzMKSXvhQAsSIIjAPgSKIfAIozkI0mRKnm2qQKSmLmCjNEBB7COMbNasEVpl7Mn3Qdvqy7_My6Arxwkp0nj0VFURrTW7lEaZJ512qMPjebnFzoQVH8.GujnmQ.yop-ovqpL2j3xfe0AmZSevt0mRM; doncusSessionId=eyJfZnJlc2giOmZhbHNlLCJ1c2VyX2lkIjoiNjgwNzAxZGRkY2E5NDE1MjJiNzdlODY0In0.GujnnA.VNsa9_0iDmjIi_f7W7zBKNTfFhU")
                    .header("cookie", Constant.cookies)
                    .ignoreContentType(true)  // 允许非HTML响应
                    .ignoreHttpErrors(true)   // 禁用HTTP错误异常
                    .maxBodySize(0)            // 解除1MB响应限制
                    .execute();

            // 输出结果
           log.info("HTTP状态码: " + response.statusCode());
           //log.info("响应内容:\n" + response.body());
            // 错误处理逻辑
            if (response.statusCode() == 401) {
                handleAuthenticationFailure(ENTERPRISE_ID);
            } else if (response.statusCode() == 403) {
                System.err.println("访问被拒绝：请确认企业ID归属权限");
            }

            //解析
            JsonParserUtil.ParseAddress(response.body());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void handleAuthenticationFailure(String enterpriseId) {
        System.err.println("认证失败可能原因：");
        System.err.println("1. Cookie已过期（需重新登录获取最新Cookie）");
        System.err.println("2. 缺少动态签名参数（对比浏览器请求头检查是否需要以下参数）");
        System.err.println("   - x-tonxis-signature");
        System.err.println("   - x-tonxis-sid");
        System.err.println("3. 企业ID参数无效（当前值：" + enterpriseId + "）");
    }
}